<template>
  <div class="login-quick-form">
    <van-form ref="quickForm" :model="quickForm" validate-first>
      <div class="form_list">
        <div class="form-label">手机号</div>
        <div class="form-con">
          <van-field v-model="quickForm.phone" name="phone" placeholder="请输入手机号" :rules="rules.phone" />
        </div>
      </div>
      <div class="form_list">
        <div class="form-label">验证码</div>
        <van-field
          v-model="quickForm.smCode"
          name="smCode"
          placeholder="请输入验证码"
          maxlength="6"
          :rules="rules.smCode">
          <template #button>
            <div class="code" @click="sendCode">{{ codeBtnDisplay }}</div>
          </template>
        </van-field>
      </div>
      <div class="commit" @click="alyOnSubmit">登录</div>
    </van-form>
  </div>
</template>

<script>
import { sendSignCode } from '@api/api-public.js'
import { phoneReg } from '@common/regExps.js'
export default {
  name: 'QuickForm',
  components: {},
  props: {},
  data() {
    return {
      quickForm: {
        phone: '',
        smCode: '',
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号' },
          {
            validator: val => {
              if (!phoneReg.test(val)) {
                return false
              }
            },
            message: '请输入有效的手机号',
          },
        ],
        smCode: [
          {
            pattern: /^\d{6}$/,
            message: '请输入有效的验证码',
          },
        ],
      },
      cutdownSec: 0,
      sendCodeStatus: 'init', // init, send, retry
      cutdownTimer: null,
    }
  },
  watch: {},
  computed: {
    codeBtnDisplay() {
      if (this.sendCodeStatus === 'init') {
        return '获取验证码'
      } else if (this.sendCodeStatus === 'send') {
        return this.cutdownSec + ' 秒'
      }
      return '重新获取'
    },
  },
  created() {},
  mounted() {
    this.$refs.quickForm.resetValidation()
  },
  methods: {
    // 发送验证码
    sendCode() {
      if (this.sendCodeStatus === 'send') {
        return
      }
      this.$refs.quickForm
        .validate('phone')
        .then(() => {
          const params = {
            oneParam: this.quickForm.phone,
            check: '1',
            sendType: '1',
            tenantId: '',
          }
          sendSignCode(params).then(res => {
            if (res.status === 200) {
              this.startCutdown()
            }
          })
        })
        .catch(err => {
          console.log('err :>> ', err)
        })
    },
    // 验证码有效期开始计时
    startCutdown() {
      this.clearCutdownTimer()
      this.sendCodeStatus = 'send'
      this.cutdownSec = 60
      this.cutdownTimer = setInterval(() => {
        this.cutdownSec--
        if (this.cutdownSec <= 0) {
          this.clearCutdownTimer()
          this.sendCodeStatus = 'retry'
        }
      }, 1000)
    },
    // 清除计时器
    clearCutdownTimer() {
      if (this.cutdownTimer) {
        clearInterval(this.cutdownTimer)
      }
    },
    alyOnSubmit() {
      this.$refs.quickForm
        .validate()
        .then(() => {
          this.$emit('login', this.quickForm)
        })
        .catch(() => {})
    },
  },
}
</script>
<style lang="scss" scoped>
.login-quick-form {
  width: 100%;
  .form_list {
    width: 100%;
    border-bottom: 1px solid #dfdfdf;
    margin-bottom: 10px;
    padding-bottom: 5px;
    .form-label {
      font-size: 16px;
      font-weight: bold;
      margin-bottom: 10px;
    }
    .form-con {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .iconfont {
        font-size: 22px;
        color: #a8d2f7;
        &.psw {
          margin-right: 20px;
        }
      }
      .van-cell {
        height: 100%;
        font-size: 14px;
        background-color: transparent;
      }
    }
    .code {
      color: #004db6;
    }
  }
  .commit {
    margin-top: 30px;
    height: 45px;
    line-height: 45px;
    border-radius: 6px;
    background: #004db6;
    color: #ffffff;
    text-align: center;
    font-weight: bold;
  }
}
</style>
